package com.lele.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lele.mapper.AdminMapper;
import com.lele.pojo.Admin;
import com.lele.pojo.Role;
import com.lele.pojo.vo.LoginAdminVo;
import com.lele.service.IAdminService;
import com.lele.utils.JWTUtil;
import com.lele.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lele
 * @since 2022-11-26
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Resource
    private AdminMapper adminMapper;
/*    @Value("${jwt.data.tokenHeader}")
    private String Authorization;*/




 /*   @Override
    public Result login(LoginAdminVo adminVo, HttpServletRequest request) {
        String username = adminVo.getUsername();
        String password = adminVo.getPassword();
        String code = adminVo.getCode();
        if(StringUtils.isEmpty(code)||!code.equals(request.getSession().getAttribute("captcha"))){
            return Result.ERROR("验证码错误");
        }
        if(StringUtils.isEmpty(username)) {
           return Result.ERROR("用户名为空");
        }
        //用户名进行比较，查出来对应的信息
        UserDetails userDetails = this.getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUsername, username).eq(Admin::getEnabled, 1));
        //使用PasswordEncoding实现密码的比较
        if(userDetails==null||!passwordEncoder.matches(password, userDetails.getPassword())){
            return Result.ERROR("用户名或密码错误");
        }
        if(!userDetails.isEnabled()){
            return Result.ERROR("该用户不可用");
        }
        //更新springsecurity登录用户对象
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String token = jwtUtil.createToken(userDetails);
        HashMap<String, String> map = new HashMap<>();
        map.put("token",token);
        map.put("tokenHead","Bearer");
        return Result.OK(map);
    }*/


    @Override
    public Admin getAdminInfo(String name) {
        Admin admin = this.getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUsername, name).eq(Admin::getEnabled, true));
        return admin;
    }

    @Override
    public Admin getAdminInfoByPrincipal(Principal principal) {
        String name = principal.getName();
        Admin admin = this.getAdminInfo(name);
        admin.setPassword(null);
        return admin;
    }

    @Override
    public List<Role> getAdminWithRoles(Integer id) {
        List<Role> roles = this.baseMapper.getAdminWithRoles(id);
        return roles;
    }

    @Override
    public List<Admin> getAllAdmin(String keywords) {
        Admin userInfo = getUserInfo();
        Integer id = userInfo.getId();
        List<Admin> adminList=adminMapper.getAllAdmin(id,keywords);
        return adminList;
    }



    public static Admin getUserInfo(){
        return (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
